<?php

class  Assets_model extends CI_Model
{
    public function __construct()
    {
        $this->load->helper("url");
        $this->load->model('assets_model');
        $this->load->model('users_model');
    }


    function  home_money($id = 0){
        //本月预估佣金
        $data['month_estimate'] = $this->commission($id,1,'已入账');
//        //累计收入佣金
        $data['total'] = $this->commission($id,0,"('已结算', '已入账')");
//        //累计提现佣金
        $data['withdraw'] = $this->commission($id,0,"('已结算')");
//        //本月提现
        $data['month_withdaw'] = $this->commission($id,1,"已结算");

        return $data;
    }

    function my_money($id){
        $info = $this->users_model->get_one($id);
        //基本工资
        $data['wage'] = $info['wage']?$info['wage']:0;
        //本月提成
        $data['month_commission'] = $this->commission($id,1,"未结算");
        //本月佣金
        $data['month_push_money'] = $this->push_money($id,1,"未结算");
        //本月收入
        $data['month_income'] = $data['wage']+$data['month_commission']+$data['month_push_money'];
        return $data;
    }

    // 我的资产 新 cj
    function my_asset($id){
        $info = $this->users_model->get_one($id);
        //基本工资
        $gz = $info['wage']?$info['wage']:0;
        $data['wage'] = number_format($gz,2);
        //本月佣金
        $yj = $this->commission($id,1,"未结算");
        $data['month_commission'] = number_format($yj,2);
        //本月奖金
        $jj = $this->users_model->manager_push_money_sum($id,date("Y"),date("m"));
        $data['month_push_money'] = number_format($jj,2);
        //本月收入
        $data['month_income'] = number_format($gz+$yj+$jj,2);
        return $data;
    }

    //佣金
    public function commission($id = 0,$time_type=0,$status=0)
    {
        if ($id) {
            //佣金
            $sql = "
                     SELECT SUM(c.commission_total) sum FROM customer_order c WHERE c.manager_id=$id  ";
            if ($status){
                $sql.=" and c.commission_status in ".$status;
            }
            if ($time_type==1){
                //本月时间
                $date = getthemonth(date("Y-m-d"));
                $date[0].=" 00:00:00";
                $date[1].=" 23:59:59";

                $sql = "SELECT sum(commission_total) sum FROM customer_order c WHERE c.manager_id = $id and payment_date >= '".$date
                    [0]."' AND payment_date <= '".$date[1]."'";
                    if ($status){
                        $sql.=" and c.commission_status='".$status."'";
                    }
            }
            $query = $this->db->query($sql);

            $info = $query->result_array();
            if ($info) {
                $data = $info[0]['sum']?$info[0]['sum']:0;
                return $data;
            } else {
                return false;
            }
        } else{
            return false;
        }
    }


    //个人奖金
    function  push_money($id = 0,$time_type=0,$status=0){
        if ($id) {
            //佣金
            $sql = "SELECT sum(commission_total) sum FROM push_money pm INNER join customer_order co ON co.id= pm.order_id WHERE pm.manager_id=$id";
            if ($status){
                $sql.=" and pm.status='".$status."'";
            }
            if ($time_type==1){
                //本月时间
                $date = getthemonth(date("Y-m-d"));
                $date[0].=" 00:00:00";
                $date[1].=" 23:59:59";

                $sql = "SELECT sum(commission_total) sum FROM push_money pm INNER join customer_order co ON co.id= pm.order_id WHERE pm.manager_id=$id and  date_add( co.completed_date, INTERVAL 7 DAY ) >= '".$date[0]."' AND date_add( co.completed_date, INTERVAL 7 DAY ) < '".$date[1]."'";
                if ($status){
                    $sql.=" and pm.status='".$status."'";
                }
            }
            $query = $this->db->query($sql);

            $info = $query->result_array();
            if ($info) {
                $data = $info[0]['sum']?$info[0]['sum']:0;
                return $data;
            } else {
                return false;
            }
        } else{
            return false;
        }
    }

    //佣金明细
    public function commission_list($id = 0,$search = "",$page=1,$limit = 8)
    {
        if ($id) {
            //佣金
            $sql = "
SELECT co.order_bn,co.id order_id,ct.customer_name,co.order_date FROM  customer_order co inner join customer ct ON co.customer_id = ct.id WHERE co.manager_id=$id and commission_status<>'未入账'";
            if($search){
                $sql.=" and (co.order_bn like '%".$search."%' or ct.customer_name like '%".$search."%' or ct.customer_tel like '%".$search."%')";
            }
            $limit_from = ($page-1)*$limit;
            $sql.=" order by order_date desc";
            $sql.=" limit ".$limit_from.",".$limit;
            $query = $this->db->query($sql);

            $info = $query->result_array();
             return $info;
        } else{
            return false;
        }
    }

    //奖金列表
    public function push_money_list ($id = 0,$search = "",$page = 1,$limit = 8)
    {
        if ($id) {
            $sql = "SELECT co.order_bn,co.id order_id,mg.name,mg.identity,pm.id push_id,co.order_date from push_money pm INNER  join customer_order co ON co.id=pm.order_id INNER join manager mg ON mg.id = co.manager_id WHERE pm.manager_id=$id";
            if($search){
                $sql.=" and (co.order_bn='".$search."' or mg.name = '".$search."')";
            }
            $sql.=" order by order_date desc";
            $limit_from = ($page-1)*$limit;
            $sql.=" limit ".$limit_from.",".$limit;
            $query = $this->db->query($sql);
            $info = $query->result_array();
            return $info;
        } else{
            return false;
        }
    }



    /**
     * 佣金详情
     * @param unknown $id
     */
    public function getOrderCommissionDetails($id){

        $data=array();

        if (empty ( $id )) {
            $data ['code'] = 10001;
        }

        if (isset($id)){
            $orderwhere=array(
                'id'=>$id
            );

            $commissionwhere=array(
                'order_id'=>$id
            );

            $queryorder=$this->db->select("`manager_id`, `customer_id`,`order_bn`, `order_money`,`commission_total`, `order_status`,`commission_status`, `order_date`, `payment_date`")->where($orderwhere)->get("customer_order");
            //退货信息
            $query_return = "select commission return_commission, money return_money,created_at return_time from commission where order_id = $id and commission<0";
            $querycommission_return=$this->db->query($query_return);
            //佣金信息
            $query = "select *  from commission where order_id = $id and commission>=0";
            $querycommission=$this->db->query($query);
            $data ['data']=array();

            if (isset ( $queryorder )) {
                $data ['code'] = 1;
                $order=$queryorder->row_array();
                $data ['data']['order'] =  $order;
                $customerwhere=array(
                    'id'=>$order['customer_id']
                );
                $querycustomer=$this->db->where($customerwhere)->get("customer");
                if (isset($querycustomer)){
                    $cus =  $querycustomer->row_array();

                    $data ['data']['order']['customer_tel'] =  $cus['customer_tel'];
                    $data ['data']['order']['customer_name'] =  $cus['customer_name'];
                }

                if (isset($querycommission)){
                    if($comm = $querycommission->result_array()){
                        foreach ($comm as &$v){
                            $v['payment_date']= $data['data']['order']['payment_date'];
                        }
                        $data['data']['commission'] = $comm;

                    }
                }
                if (isset($querycommission_return)){
                   $returns = $querycommission_return->result_array();
                }
                $data['data']['returns'] = $returns;

//                //入账时间结果
//                $withdraw_where = array(
//                    "target_id" =>$order['id'],
//                    'withdraw_type'=>1
//                );
//                $withdraw=$this->db->where($withdraw_where)->get("withdraw");
//                if (isset($withdraw)){
//                    if($withdraw_data = $withdraw->row_array()){
//                        $data['data']['withdraw_status'] = $withdraw_data['withdraw_status'];
//                        $data['data']['withdraw_time'] = $withdraw_data['created_at'];
//                    }
//                }

            } else {
                $data ['code'] = 20000;
            }
        }
        if ($data['code'] == 1) {
            $data['status'] = "success";
        } else {
            $data['status'] = "fail";
        }
        $data ['msg'] = $this->config->item ( $data ['code'], 'error' );

        return $data;
    }

    /**
     * 奖金详情
     * @param unknown $id
     */
    public function getOrderPushDetails($id)
    {

        $data = array();

        if (empty ($id)) {
            $data ['code'] = 10001;
        }
        if (isset($id)) {
            $pushwhere = array(
                'id' => $id
            );

            $querypush = $this->db->select("`id`,`order_id`,`money`, `push_money`, `status`, `created_at`")->where($pushwhere)->get("push_money");

            $data ['data'] = array();

            if (isset ($querypush)) {
                $push = $querypush->row_array();
                $orderwhere = array(
                    "id" => $push['order_id']
                );

                $queryorder = $this->db->select("`manager_id`, `customer_id`,`order_bn`,`completed_date`, `order_money`")->where($orderwhere)->get("customer_order");
                $order = $queryorder->row_array();
                $data['data'] = $order;

                $data ['data']['push_money'] = $push['push_money'];

                $data ['code'] = 1;
                $customerwhere = array(
                    'id' => $order['customer_id']
                );
                $mgwhere = array(
                    "id" => $order['manager_id']
                );
                $querycustomer = $this->db->where($customerwhere)->get("customer");


                $querymg = $this->db->where($mgwhere)->get("manager");
                if (isset($querymg)) {
                    $mg = $querymg->row_array();
                    $data ['data']['mg_name'] = $mg['name'];
                }
                if (isset($querycustomer)) {
                    $cus = $querycustomer->row_array();

                    $data ['data']['customer_tel'] = $cus['customer_tel'];
                    $data ['data']['customer_name'] = $cus['customer_name'];
                }

                //入账时间结果
                $withdraw_where = array(
                    "target_id" => $push['id'],
                    'withdraw_type' => 2,
                    'withdraw_status' => '成功'
                );
                $withdraw = $this->db->where($withdraw_where)->get("withdraw");
                $withdraw_data = $withdraw->row_array();
//                $data['data']['withdraw_status'] = $withdraw_data['withdraw_status'];
                $data['data']['withdraw_time'] = $withdraw_data['created_at'];
            } else {
                $data ['code'] = 20000;
            }


            if ($data['code'] == 1) {
                $data['status'] = "success";
            } else {
                $data['status'] = "fail";
            }


        }
        $data ['msg'] = $this->config->item($data ['code'], 'error');

        return $data;
    }
}